<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleAdd">新增</a-button>
      </template>
      <template #isUse="{ text }">
        {{ text === 1 ? '已禁用' : '已启用' }}
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
              color: 'success',
            },
            {
              label: record.status === 1 ? '启用' : '禁用',
              onClick: handleChangeStatus.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>

    <UserModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts">
  import { BasicTable, TableAction, useTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { defineComponent, ref } from 'vue';
  import UserModal from './cityModal.vue';
  import { columns, searchFormSchema } from './cityManage.data';
  import { ListApi, UpdateState } from '/@/api/cityManage/CityManageService';
  import { message } from 'ant-design-vue';

  export default defineComponent({
    components: {
      BasicTable,
      UserModal,
      TableAction,
    },
    setup() {
      const bool = ref(true);

      // 新增或编辑对话框
      const [registerModal, { openModal }] = useModal();

      const [registerTable, { reload }] = useTable({
        api: ListApi,
        useSearchForm: true,
        formConfig: {
          labelWidth: 60,
          schemas: searchFormSchema,
        },
        columns: columns,
        clickToRowSelect: false,
      });
      // 编辑
      const handleEdit = (record) => {
        console.log(record);
        // 获取编辑行的数据
        openModal(true, {
          isUpdate: true,
          record: JSON.parse(JSON.stringify(record)),
        });
      };
      // 修改状态
      const handleChangeStatus = (record) => {
        const status = record.status === 1 ? 0 : 1;
        const param = {
          id: record.id,
          status,
        };
        UpdateState(param).then((res) => {
          message.success(res.msg);
          reload();
        });
      };
      // 单个删除
      const handleDelete = (record) => {
        console.log(record);
      };
      // 新增
      const handleAdd = () => {
        openModal(true, {
          isUpdate: false,
        });
      };
      // 新增成功
      const handleSuccess = () => {
        reload();
      };
      return {
        registerTable,
        registerModal,
        handleEdit,
        handleChangeStatus,
        handleDelete,
        handleAdd,
        handleSuccess,
        bool,
      };
    },
  });
</script>

<style scoped lang="less"></style>
